package com.lyf.boot.modules.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.lyf.boot.annotation.Log;
import com.lyf.boot.core.model.IdArrDto;
import com.lyf.boot.core.model.IdDto;
import com.lyf.boot.core.model.R;
import com.lyf.boot.enums.BusinessType;
import com.lyf.boot.modules.system.entity.dto.SysPermDto;
import com.lyf.boot.modules.system.entity.vo.SysPermVo;
import com.lyf.boot.modules.system.service.ISysPermService;
import com.lyf.boot.validate.group.AddGroup;
import com.lyf.boot.validate.group.EditGroup;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 系统菜单结构
 * @author Luyufan
 * @date 2023/7/7 11:02
 */
@RestController
@RequestMapping("/sysPerm")
@RequiredArgsConstructor
public class SysPermController {

    private final ISysPermService sysPermService;

    /**
     * 获取权限列表
     * @return
     */
    @SaCheckPermission("sys:perm:list")
    @PostMapping("/getSysPermList")
    public R<List<SysPermVo>> getSysPermList(){
        return R.ok(sysPermService.getSysPermList());
    }

    /**
     * 获取权限详情
     * @param dto 请求参数
     * @return 详情数据
     */
    @PostMapping("/getSysPermInfo")
    public R<SysPermVo> getSysPermInfo(@RequestBody @Valid IdDto dto){
        return R.ok(sysPermService.getSysPermInfo(dto.getId()));
    }

    /**
     * 添加权限
     * @param dto 请求数据
     * @return 保存结果
     */
    @SaCheckPermission("sys:perm:add")
    @PostMapping("/saveSysPerm")
    @Log(module = "权限管理", business = "添加权限", businessType = BusinessType.INSERT)
    public R<Void> saveSysPerm(@RequestBody @Validated(AddGroup.class) SysPermDto dto){
        sysPermService.saveSysPerm(dto);
        return R.ok();
    }

    /**
     * 修改权限
     * @param dto 请求数据
     * @return 修改结果
     */
    @SaCheckPermission("sys:perm:update")
    @PostMapping("/updateSysPerm")
    @Log(module = "权限管理", business = "修改权限", businessType = BusinessType.UPDATE)
    public R<Void> updateSysPerm(@RequestBody @Validated(EditGroup.class) SysPermDto dto){
        sysPermService.updateSysPerm(dto);
        return R.ok();
    }

    /**
     * 删除权限
     * @param dto 请求数据
     * @return 删除结果
     */
    @SaCheckPermission("sys:perm:delete")
    @PostMapping("/deleteSysPerm")
    @Log(module = "权限管理", business = "删除权限", businessType = BusinessType.DELETE)
    public R<Void> deleteSysPerm(@RequestBody @Valid IdArrDto dto){
        sysPermService.deleteSysPerm(dto.getIds());
        return R.ok();
    }
}
